<?php
session_start();
Header("Pragma: No-cache");
set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
$PEAR_PATH = 'PEAR.php';
include ('Request/Request.php');
include "functions.php";
$lang = getLang();
$versionInt = getClientIntVersion();

global $viewstate;
global $postback;
global $cookie;

//get page of selected trackable item
$url = "http://www.geocaching.com/track/details.aspx?guid=".$_GET['guid'];
$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
$req->sendRequest();
$response = $req->getResponseBody();

//error if cache is archived or disabled

$position = strpos($response, '__EVENTARGUMENT');
$eventargument = parseBetween('value=', '/>');
$eventargument = trim(str_replace('"', '', $eventargument));
$position = strpos($response, '__VIEWSTATEFIELDCOUNT');
$viewstatefieldcount = parseBetween('value=', '/>');
$viewstatefieldcount = trim(str_replace('"', '', $viewstatefieldcount));
$position = strpos($response, '__VIEWSTATE"');
$viewstate = parseBetween('value=', '/>');
$viewstate = trim(str_replace('"', '', $viewstate));
$position = strpos($response, '__VIEWSTATE1"');
$viewstate1 = parseBetween('value=', '/>');
$viewstate1 = trim(str_replace('"', '', $viewstate1));
$position = strpos($response, '__VIEWSTATE2"');
$viewstate2 = parseBetween('value=', '/>');
$viewstate2 = trim(str_replace('"', '', $viewstate2));
$position = strpos($response, '__VIEWSTATE3"');
$viewstate3 = parseBetween('value=', '/>');
$viewstate3 = trim(str_replace('"', '', $viewstate3));
$eventtarget = 'ctl00$ContentBody$TravelBugLog$lnkViewAll';

$req = & new HTTP_Request($url);
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
$req->addPostData('__EVENTARGUMENT', $eventargument);
$req->addPostData('__VIEWSTATEFIELDCOUNT', $viewstatefieldcount);
$req->addPostData('__VIEWSTATE', $viewstate);
$req->addPostData('__VIEWSTATE1', $viewstate1);
$req->addPostData('__VIEWSTATE2', $viewstate2);
$req->addPostData('__VIEWSTATE3', $viewstate3);
$req->addPostData('__EVENTTARGET', $eventtarget);
$req->sendRequest();
$response = $req->getResponseBody();

//echo $response;

$inName = removeHTML(trim(parseBetween('<span id="ctl00_ContentBody_lbHeading">', '</span')), TRUE);
$position = strPos($response, 'ctl00_ContentBody_BugDetails_BugOwner');
$inOwner = removeHTML(trim(parseBetween('">', '</a>')), TRUE);
$inReleased = removeHTML(trim(parseBetween('<strong>Released:</strong>', '</span>')), TRUE);
$inOrigin = removeHTML(trim(parseBetween('<strong>Origin</strong>:', '/span>')), TRUE);
$inCode = removeHTML(trim(parseBetween('<span id="ctl00_ContentBody_BugDetails_BugTBNum" style="font-weight:bold;">', '</span>')), TRUE);
$inGoal = removeHTML(trim(parseBetween('<span id="ctl00_ContentBody_BugDetails_BugGoal">', '</span>')), TRUE);
$inAbout = removeHTML(trim(parseBetween('<span id="ctl00_ContentBody_BugDetails_BugDetails">', '</span>')), TRUE);

$html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
                <head>
                    <title>'.$inName.'</title>
                </head>
                <body>';

if ($_GET['show'] == 'desc') {
    $html .= '<p>'.$inGoal.'</p><p>'.$inAbout.'</p>';
}

$html .= '</body></html>';
echo $html;
?>
